home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / DrawView.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  11.2 KB  |  442 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawView.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Laurent Delamare
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRAWVIEW_H
  15. #include "DrawView.h"
  16. #endif
  17.  
  18. #ifndef DRAWDEF_H
  19. #include "DrawDef.h"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef DRAWFRM_H
  27. #include "DrawFrm.h"
  28. #endif
  29.  
  30. #ifndef EDITCMDS_H
  31. #include "EditCmds.h"
  32. #endif
  33.  
  34. #ifndef DRAWSEL_H
  35. #include "DrawSel.h"
  36. #endif
  37.  
  38. #ifndef BASESHP_H
  39. #include "BaseShp.h"
  40. #endif
  41.  
  42. #ifndef BOUNDSHP_H
  43. #include "BoundShp.h"
  44. #endif
  45.  
  46. #ifndef LINESHP_H
  47. #include "LineShp.h"
  48. #endif
  49.  
  50. #ifndef OVALSHP_H
  51. #include "OvalShp.h"
  52. #endif
  53.  
  54. #ifndef RECTSHP_H
  55. #include "RectShp.h"
  56. #endif
  57.  
  58. #ifndef RRECTSHP_H
  59. #include "RRectShp.h"
  60. #endif
  61.  
  62. #ifndef TEXTSHP_H
  63. #include "TextShp.h"
  64. #endif
  65.  
  66. #ifndef DRAWPRXY_H
  67. #include "DrawPrxy.h"
  68. #endif
  69.  
  70. #ifndef SHPTRAKR_H
  71. #include "ShpTrakr.h"
  72. #endif
  73.  
  74. #ifndef DRAWCMDS_H
  75. #include "DrawCmds.h"
  76. #endif
  77.  
  78. #ifndef DRAWCLIP_H
  79. #include "DrawClip.h"
  80. #endif
  81.  
  82. #ifndef UTILS_H
  83. #include "Utils.h"
  84. #endif
  85.  
  86. #ifndef DRWDDCMD_H
  87. #include "DrwDDCmd.h"
  88. #endif
  89.  
  90. // ----- Framework Layer -----
  91.  
  92. #ifndef FWUTIL_H
  93. #include "FWUtil.h"
  94. #endif
  95.  
  96. #ifndef FWPRESEN_H
  97. #include "FWPresen.h"
  98. #endif
  99.  
  100. #ifndef FWSCROLR_H
  101. #include "FWScrolr.h"
  102. #endif
  103.  
  104. #ifndef FWSCLBAR_H
  105. #include "FWSclBar.h"
  106. #endif
  107.  
  108. #ifndef FWGROWBX_H
  109. #include "FWGrowBx.h"
  110. #endif
  111.  
  112. // ----- OS Layer -----
  113.  
  114. #ifndef FWMENU_H
  115. #include "FWMenu.h"
  116. #endif
  117.  
  118. #ifndef FWRECSHP_H
  119. #include "FWRecShp.h"
  120. #endif
  121.  
  122. #ifndef FWCURSOR_H
  123. #include "FWCursor.h"
  124. #endif
  125.  
  126. #ifndef FWEVENT_H
  127. #include "FWEvent.h"
  128. #endif
  129.  
  130. #ifndef FWODGEOM_H
  131. #include "FWODGeom.h"
  132. #endif
  133.  
  134. #ifndef FWLINSHP_H
  135. #include "FWLinShp.h"
  136. #endif
  137.  
  138. #ifndef FWBMPSHP_H
  139. #include "FWBmpShp.h"
  140. #endif
  141.  
  142. // ----- OpenDoc Includes -----
  143.  
  144. #ifndef SOM_Module_OpenDoc_Commands_defined
  145. #include <CmdDefs.xh>
  146. #endif
  147.  
  148. #ifndef SOM_Module_OpenDoc_StdProps_defined
  149. #include <StdProps.xh>
  150. #endif
  151.  
  152. #ifndef SOM_ODSession_xh
  153. #include <ODSessn.xh>
  154. #endif
  155.  
  156. #ifndef SOM_ODDispatcher_xh
  157. #include <Disptch.xh>
  158. #endif
  159.  
  160. //========================================================================================
  161. // RunTime Info
  162. //========================================================================================
  163.  
  164. #ifdef FW_BUILD_MAC
  165. #pragma segment odfdrawframes
  166. #endif
  167.  
  168. FW_DEFINE_CLASS_M1(CDrawView, FW_CView);
  169.  
  170. //========================================================================================
  171. // CDrawView
  172. //========================================================================================
  173.  
  174. //----------------------------------------------------------------------------------------
  175. // CDrawView::CDrawView
  176. //----------------------------------------------------------------------------------------
  177.  
  178. CDrawView::CDrawView(Environment* ev, CDrawFrame* frame, FW_CRect& bounds, CDrawPart* drawPart) :
  179.     FW_CView(ev, (FW_CView*)frame, bounds),
  180.     fDrawPart(drawPart),
  181.     fDrawFrame(frame)
  182. {    
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // CDrawView::~CDrawView
  187. //----------------------------------------------------------------------------------------
  188.  
  189. CDrawView::~CDrawView()
  190. {
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // CDrawView::Draw
  195. //----------------------------------------------------------------------------------------
  196.  
  197. void CDrawView::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)
  198. {
  199.     // Render inside the content view
  200.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  201.     FW_CRect invalidRect;
  202.     vc.GetClipRect(invalidRect);
  203.  
  204.     FW_CRectShape::RenderRect(vc, invalidRect, FW_kFill, FW_kWhiteEraseInk);
  205.  
  206.     // ----- Determine what to Render -----
  207.     ODCanvas* canvas = odFacet->GetCanvas(ev);
  208.     FW_Boolean dynamicCanvas = canvas->IsDynamic(ev);
  209.  
  210.     // ----- Render the grid -----
  211.     if (dynamicCanvas)
  212.         DrawGrid(ev, vc, invalidRect);
  213.  
  214.     // ----- Render every shape -----
  215.     FW_CRect updateBox;
  216.     FW_COrderedCollectionIterator ite(fDrawPart->GetShapeList());
  217.     for (CBaseShape* theShape = (CBaseShape*)ite.First(); ite.IsNotComplete(); theShape = (CBaseShape*)ite.Next())
  218.     {
  219.         theShape->GetUpdateBox(updateBox);
  220.         if (invalidRect.IsIntersecting(updateBox))
  221.             theShape->RenderShape(ev, odFacet, vc);
  222.     }
  223.  
  224.     // ----- Render selections Handles -----
  225.     if (IsActive(ev) && dynamicCanvas)    {    // Do not render handles for print / print preview
  226.         fDrawFrame->GetSelection(ev)->RenderSelectionHandles(ev, vc);
  227.     }
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // CDrawView::AdjustCursor
  232. //----------------------------------------------------------------------------------------
  233.  
  234. FW_Boolean CDrawView::AdjustCursor(Environment *ev, ODFacet* facet, const FW_CPoint& where)
  235. {
  236.     FW_Boolean cursorAdjusted = FW_CView::AdjustCursor(ev, facet, where);
  237.     
  238.     if (!cursorAdjusted && 
  239.         fDrawPart->GetActiveFrame(ev) == (FW_CFrame*)fDrawFrame && 
  240.             fDrawPart->GetTool() != kSelectTool)
  241.     {
  242.         FW_gCrossHairCursor.Select();
  243.         cursorAdjusted = TRUE;
  244.     }
  245.  
  246.     return cursorAdjusted;
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. // CDrawView::DoMouseDown
  251. //----------------------------------------------------------------------------------------
  252.  
  253. FW_Boolean CDrawView::DoMouseDown(Environment *ev, const FW_CMouseEvent& theMouseEvent)
  254. {
  255.     ODFacet*         theFacet = theMouseEvent.GetFacet(ev);
  256.     CDrawSelection*    selection = fDrawFrame->GetSelection(ev);
  257.     
  258.     if (fDrawPart->GetTool() == kSelectTool)
  259.     {
  260.         if (selection->IsMouseInDraggableItem(ev, fDrawFrame, theMouseEvent, FALSE))
  261.         {
  262.             fDrawFrame->Drag(ev, theMouseEvent);
  263.         }
  264.         else if (!selection->Resize(ev, theMouseEvent))
  265.         {
  266.             selection->SelectWithRectangle(ev, theMouseEvent);
  267.         }
  268.                             
  269.         return TRUE;
  270.     }
  271.     
  272.     // ----- If read only don't want to create a new shape -----
  273.     if (fDrawPart->IsReadOnly(ev))
  274.     {
  275.         FW_Beep();
  276.         return TRUE;
  277.     }
  278.     
  279.     // ----- Create a new shape -----
  280.     CBaseShape *theShape =  fDrawPart->NewShape(ev, fDrawPart->GetTool() - kLine + kLineShape);
  281.     
  282.     FW_TRY
  283.     {
  284.         FW_PStyle trackStyle(fDrawPart->GetRenderVerb() == kFillOnly ? FW_kFixedPos1 : fDrawPart->GetPenSize(), FW_kGrayPat);
  285.         FW_PInk trackInk(FW_kRGBBlack, FW_kRGBWhite, FW_kXOr);
  286.         theShape->ChangeRenderVerb(ev, fDrawPart, kFrameOnly);
  287.         theShape->SetFrameInk(trackInk);
  288.         theShape->SetFrameStyle(trackStyle);
  289.         
  290.         CShapeTracker tracker(ev, this, theFacet, theShape, fDrawPart->IsAutoGridOn());
  291.         if (tracker.Track(ev, theMouseEvent))
  292.         {
  293.             theShape->SetFrameInk(FW_kNormalInk);
  294.             theShape->SetFrameStyle(FW_kNormalStyle);
  295.             
  296.             FW_CColor color;
  297.     
  298.             theShape->ChangeRenderVerb(ev, fDrawPart, fDrawPart->GetRenderVerb());
  299.             theShape->ChangePenSize(ev, fDrawPart, fDrawPart->GetPenSize());
  300.                     
  301.             fDrawPart->GetFrameColor(color);
  302.             theShape->ChangeFrameColor(ev, fDrawPart, color);
  303.     
  304.             fDrawPart->GetFillColor(color);
  305.             theShape->ChangeFillColor(ev, fDrawPart, color);
  306.     
  307.             theShape->ChangeFramePattern(ev, fDrawPart, fDrawPart->GetFramePattern());
  308.             theShape->ChangeFillPattern(ev, fDrawPart, fDrawPart->GetFillPattern());
  309.             
  310.             CDrawShapeCommand* cmd = FW_NEW(CDrawShapeCommand,
  311.                                             (ev, fDrawPart, fDrawFrame, selection, theShape));
  312.             fDrawPart->ExecuteCommand(ev, cmd);
  313.             
  314.             FW_CRect dragRect;
  315.             theShape->GetDragRect(dragRect);
  316.  
  317.             FW_CAcquiredODShape aqShape = ::FW_NewODShape(ev, dragRect);
  318.             
  319.             CDrawFacetClipper facetClipper(ev, fDrawPart);
  320.             facetClipper.Clip(ev, fDrawFrame->GetPresentation(ev), aqShape);
  321.             
  322.             FW_CViewContext vc(ev, this, theMouseEvent.GetFacet(ev));
  323.             theShape->RenderShape(ev, theFacet, vc);
  324.             
  325.             theShape->GetUpdateBox(ev, aqShape);
  326.             fDrawFrame->GetPresentation(ev)->Invalidate(ev, aqShape, theFacet);        // will not invalide theFacet
  327.         }
  328.         else
  329.         {
  330.             delete theShape;
  331.         }
  332.     }
  333.     FW_CATCH_BEGIN
  334.     FW_CATCH_EVERYTHING()
  335.     {
  336.         delete theShape;
  337.         FW_THROW_SAME();
  338.     }
  339.     FW_CATCH_END
  340.     
  341.     return TRUE;
  342. }
  343.  
  344. //----------------------------------------------------------------------------------------
  345. // CDrawView::DoMouseDownInEmbeddedFrameBorder
  346. //----------------------------------------------------------------------------------------
  347.  
  348. FW_Boolean CDrawView::DoMouseDownInEmbeddedFrameBorder(Environment *ev, const FW_CBorderMouseEvent& theBorderMouseEvent)
  349. {    
  350.     fDrawPart->SetTool(ev, kSelectTool);
  351.  
  352.     GetFrame(ev)->Drag(ev, theBorderMouseEvent);
  353.     
  354.     return TRUE;
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // CDrawView::DoMouseDownInEmbeddedFrame
  359. //----------------------------------------------------------------------------------------
  360.  
  361. FW_Boolean CDrawView::DoMouseDownInEmbeddedFrame(Environment* ev, 
  362.                                                 const FW_CEmbeddedMouseEvent& theEmbeddedMouseEvent)
  363. {
  364.     FW_Boolean handled = TRUE;
  365.     
  366.     if (theEmbeddedMouseEvent.GetNumberOfClicks(ev) > 1)
  367.         GetFrame(ev)->OpenSelection(ev);
  368.     else
  369.         handled = DoMouseDown(ev, theEmbeddedMouseEvent);
  370.     
  371.     return handled;
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // CDrawView::DrawGrid
  376. //----------------------------------------------------------------------------------------
  377.  
  378. void CDrawView::DrawGrid(Environment *ev, FW_CGraphicContext& gc, const FW_CRect& invalidRect)
  379. {
  380.     if (fDrawFrame->IsGraphicsGridOn())
  381.     {
  382.         FW_BitPattern vertical = {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  383.         FW_BitPattern horizontal = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
  384.         
  385.         FW_PPattern verticalPat(vertical);
  386.         FW_PPattern horizontalPat(horizontal);
  387.         
  388.         FW_CFixed minY = FW_IntToFixed(72 * (invalidRect.top.AsInt() / 72));
  389.         FW_CFixed maxY = FW_IntToFixed(72 * ((invalidRect.bottom + FW_kFixed72).AsInt() / 72));
  390.         FW_CFixed minX = FW_IntToFixed(72 * (invalidRect.left.AsInt() / 72));
  391.         FW_CFixed maxX = FW_IntToFixed(72 * ((invalidRect.right + FW_kFixed72).AsInt() / 72));
  392.         
  393.         // skip top-most and left-most lines
  394.         FW_CFixed startX = (minX == FW_kFixed0) ? FW_kFixed72 : minX;
  395.         FW_CFixed startY = (minY == FW_kFixed0) ? FW_kFixed72 : minY;
  396.         
  397.         FW_PStyle lineStyle(FW_IntToFixed(0), verticalPat);
  398.         FW_CLineShape line(startX, minY, startX, maxY, FW_kNormalInk, lineStyle);
  399.  
  400.         for (FW_CFixed x = startX; x <= maxX; x += FW_kFixed72)
  401.         {
  402.             line.Render(gc);
  403.             line.MoveShape(FW_kFixed72, FW_IntToFixed(0));
  404.         }
  405.         
  406.         line.GetStyle()->SetPattern(horizontalPat);
  407.         line.SetLineStart(minX, startY);
  408.         line.SetLineEnd(maxX, startY);
  409.         
  410.         for (FW_CFixed y = startY; y <= maxY; y += FW_kFixed72)
  411.         {
  412.             line.Render(gc);
  413.             line.MoveShape(FW_IntToFixed(0), FW_kFixed72);
  414.         }
  415.     }
  416. }
  417.  
  418. //----------------------------------------------------------------------------------------
  419. //    CDrawView::Activate
  420. //----------------------------------------------------------------------------------------
  421.  
  422. FW_Boolean CDrawView::Activate(Environment* ev)
  423. {
  424.     FW_Boolean newState = FW_CView::Activate(ev);
  425.     
  426.     if (newState) {
  427.         fDrawFrame->GetSelection(ev)->RenderAllHandles(ev, fDrawFrame);
  428.     }
  429.         
  430.     return newState;
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. //    CDrawView::Deactivate
  435. //----------------------------------------------------------------------------------------
  436.  
  437. void CDrawView::Deactivate(Environment* ev)
  438. {
  439.     FW_CView::Deactivate(ev);
  440.     fDrawFrame->GetSelection(ev)->RenderAllHandles(ev, fDrawFrame);
  441. }
  442.